//
//  Dock.swift
//  QQ空间
//
//  Created by 叶竞泽 on 2017/5/8.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import UIKit

class DockView: UIView {
    
    // MARK: - 属性
    
    /// 设备在不同方向时本view的高度
    var screenHeight: CGFloat {
        var height: CGFloat = 0
        switch UIDevice.current.orientation {
        case .portraitUpsideDown: fallthrough
        case .portrait:
            height = max(inView.bounds.height, inView.bounds.width)
        case .landscapeLeft: fallthrough
        case .landscapeRight:
            height = min(inView.bounds.height, inView.bounds.width)
        default:
            break
        }
        return height
    }
    
    /// 设备在不同方向时本view的宽度
    var screenWidth: CGFloat {
        var width: CGFloat = 0
        switch UIDevice.current.orientation {
        case .portraitUpsideDown: fallthrough
        case .portrait:
            width = min(inView.bounds.height, inView.bounds.width) * percentWidthInPortrait
        case .landscapeLeft: fallthrough
        case .landscapeRight:
            width = max(inView.bounds.height, inView.bounds.width) * percentWidthInLandscape
        default:
            break
        }
        return width
    }

    /// 当前设备方向
    var deviceOrientation: UIDeviceOrientation {
        return UIDevice.current.orientation
    }
    
    /// 父视图
    var inView: UIView
    
    /// 底部工具视图
    var bottomMenuView: BottomMenuView?
    /// 中间工具视图
    var centerMenuView: CenterMenuView?
    /// 顶部个人信息视图
    var topInfoView: TopMenuView?
    
    /// 宽度在父视图的比例(竖屏)
    private var percentWidthInPortrait: CGFloat = 0.06
    /// 宽度在父视图的宽度(横屏)
    private var percentWidthInLandscape: CGFloat = 0.3

    // MARK: - 生命周期
    
    /// 初始化
    ///
    /// - Parameter inView: 传递的父视图
    init(inView: UIView) {
        // 设置父视图
        self.inView = inView
        // 初始化
        super.init(frame: CGRect.zero)
        frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
        // 背景色
        backgroundColor = UIColor.red
        // 监听屏幕旋转
        NotificationCenter.default.addObserver(self, selector: #selector(changeOrientation), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
        // 设置边栏
        setupBottomMenuView()
        setupCenterMenuView()
        setupTopMenuView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    // MARK: - 方法
    
    /// 监听设备旋转
    @objc private func changeOrientation() {
        var w: CGFloat = 0
        var h: CGFloat = 0
        switch UIDevice.current.orientation {
        case .portrait: fallthrough
        case .portraitUpsideDown:
            w = inView.bounds.width * percentWidthInPortrait
            h = max(inView.bounds.height, inView.bounds.width)
        case .landscapeLeft: fallthrough
        case .landscapeRight:
            w = inView.bounds.width * percentWidthInLandscape
            h = min(inView.bounds.height, inView.bounds.width)
        default:
            break
        }
        frame = CGRect(x: 0, y: 0, width: w, height: h)
    }
}

// MARK: - 设置界面
extension DockView {
    /// 设置底部工具视图
    fileprivate func setupBottomMenuView() {
        bottomMenuView = BottomMenuView(inView: self)
        bottomMenuView?.backgroundColor = UIColor.black
        addSubview(bottomMenuView!)
    }
    /// 设置中间工具视图
    fileprivate func setupCenterMenuView() {
        centerMenuView = CenterMenuView(inView: self, bottomView: bottomMenuView!)
        centerMenuView?.backgroundColor = UIColor.black
        addSubview(centerMenuView!)
    }
    /// 设置顶部视图
    fileprivate func setupTopMenuView() {
        topInfoView = TopMenuView(inView: self, centerView: centerMenuView, bottomView: bottomMenuView)
        topInfoView?.backgroundColor = UIColor.black
        addSubview(topInfoView!)
    }
}
